<?php
if ( ! defined('ROOT')) exit('No direct script access allowed');

/**
 * 后台首页类
 * @author gzute
 * @link http://www.gzute.com
 * @E_mail gzute@qq.com
 */
Base::load_app_class('admin','admin');

class Index extends Admin
{
	private $captcha;
	private $form;
	public function __construct()
	{
		Base::load_model('admin');
		$this->captcha = Base::load_sys_class('captcha');
		$this->form = Base::load_sys_class('validation');
	}
	
	public function initAction()
	{
		Base::load_app_func('menus');
		$menu_json = json_encode(menus());
		$visitor['user_name'] = self::get_session('admin_name');
		self::tpl_assign('menu_json',$menu_json);
		self::tpl_assign('back_nav',menus());
		self::tpl_assign('visitor',$visitor);
		self::tpl_display('index');
	}
	
	public function welcomeAction()
	{
		$config = Base::load_config('config');
		$sys_info = array(
            'server_os' => $_ENV['OS'],
            'web_server' => $_SERVER['SERVER_SOFTWARE'],
            'php_version' => PHP_VERSION,
            'mysql_version' => mysql_get_server_info(),
            'share_version' => VERSION,
            'install_date' => $config['install_date']
        );
        self::tpl_assign('sys_info',$sys_info);
        $uid = self::get_session('admin_id');
        $visitor = AdminModel::findByid($uid);
        self::tpl_assign('visitor',$visitor);
        self::tpl_display('welcome');
	}
	
	public function loginAction()
	{
		$rules = array(
			array(
				'field' => 'username',
				'label' => 'lang:username',
				'rules' => 'required'
			),
			array(
				'field' => 'password',
				'label' => 'lang:password',
				'rules' => 'required'
			),
			array(
				'field' => 'captcha',
				'label' => 'lang:captcha',
				'rules' => 'required'
			)
		);
		$this->form->set_rules($rules);
		if(self::post('dosubmit',TRUE))
		{
			if($this->captcha->checkCaptcha())
			{
				if($this->form->run() == FALSE)
				{
					sys_msg($this->form->error_string());
				}else{
					if(AdminModel::login())
					{
						$links = array(
	                        array(
	                            'text' => lang('turn_back_control'),
	                            'href' => site_url(array('m'=>'admin'))
	                        ),
                    	);
                    	sys_msg(lang('login_success'),$links);
					}else {
						sys_msg(lang('login_fail'));
					}
				}
			}else{
				sys_msg(lang('captcha_error'));
			}
		}else{
			$captcha_url = site_url(array('m'=>'admin','c'=>'index','a' => 'captcha'));
			self::tpl_assign('captcha_url',$captcha_url);
			self::tpl_display('login');
		}
	}
	
	public function loginoutAction()
	{
		self::session_destroy();
        redirect('?m=admin', 'refresh');
	}
	
	public function captchaAction()
	{
		$code = $this->captcha->returnText();
		self::set_session('captcha', $code);
		$this->captcha->create();
	}
}

//end of this file